package com.loyal.loyalerp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loyal.loyalerp.model.GyRolePermission;
import com.loyal.loyalerp.model.vo.ListRoleVO;
import com.loyal.loyalerp.service.RolePermissionService;

@Controller
public class RolePermissionController {
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	//【4】通过用户角色编辑修改角色的权限：editRoleToPermission，接受的参数有roleid，字符串数组chks
	@RequestMapping("editRoleToPermission")
	public @ResponseBody void editRoleToPermission(Integer roleid,Integer[] chks) {
       if(roleid>=0&&chks!=null) {
    	   try {
			rolePermissionService.editRoleToPermission(roleid, chks);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
	//【5】通过权限id去编辑修改用户role
	@RequestMapping("editPermissionToRole")
	public @ResponseBody void editPermissionToRole(Integer pid,Integer[] chks) {
		System.out.println(chks[0]+" "+chks[1]);
		try {
			rolePermissionService.editPermissionToRole(pid, chks);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//【1】跳转到WEB-INF/jsp/RolePermission/role.jsp
	@RequestMapping("getRole")
	public String getRole() {
		return "RolePermission/role";
	}
	//【2】跳转到WEB-INF/jsp/RolePermission/permission.jsp
		@RequestMapping("getPermission")
		public String getrolePermission() {
			return "RolePermission/permission";
	  }
		
	 //【3】查询所有role和permission
	@RequestMapping("rolePermission")
	public @ResponseBody List<Object> changeRolePermission() {
		try {
			//封装了所有用户role和所有用户permission
			return rolePermissionService.queryByRoles();
		} catch (Exception e) {
			e.printStackTrace();
			//跳转到错误页面
	      return null;
		}
	}
	
	@RequestMapping("userRole")
	public @ResponseBody ListRoleVO listUser() {
		try {
			return rolePermissionService.listUser();
		} catch (Exception e) {
			e.printStackTrace();
	      return null;
		}
	}
	
	//【3】参数roleid查询
	@RequestMapping("getRoleIdToQuery")
	public @ResponseBody List<GyRolePermission> getRoleIdToQuery(Integer roleid) {
		try {
			return rolePermissionService.getRoleIdToQuery(roleid);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//【4】参数permissionid查询
		@RequestMapping("getpermissionIdToQuery")
		public @ResponseBody List<GyRolePermission> getpermissionIdToQuery(Integer permissionid) {
			try {
				return rolePermissionService.getpermissionIdToQuery(permissionid);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			
		}
		
		
		@RequestMapping("roleToUser")
		public @ResponseBody String roleToUser(Integer uid,Integer[] roleids) {
			return rolePermissionService.roleToUser(uid, roleids);
			 
		}
	

}
